package com.spring.campus_express_delivery.service.serviceImpl;

import com.spring.campus_express_delivery.dao.ExpressCompanyDao;
import com.spring.campus_express_delivery.entity.ExpressCompany;
import com.spring.campus_express_delivery.entity.PageBean;
import com.spring.campus_express_delivery.service.ExpressCompanyService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

@Service
@Transactional
public class ExpressCompanyServiceImpl implements ExpressCompanyService{
    @Resource
    ExpressCompanyDao expressCompanyDao;

    @Override
    public PageBean<ExpressCompany> selectExpressCompanyByPage(int size, int page, String sort, String asc) {
        HashMap<String,Object> map = new HashMap<String,Object>();
        PageBean<ExpressCompany> pageBean = new PageBean<ExpressCompany>();
        //封装当前页数
        pageBean.setCurrPage(page);
        //封装页数大小
        pageBean.setPageSize(size);
        //封装排序字段
        pageBean.setPageSort(sort);
        //封装排序规则
        pageBean.setPageAsc(asc);

        //封装总记录数
        int totalCount = expressCompanyDao.selectCountByExpressCompany();
        pageBean.setTotalCount(totalCount);

        //封装总页数
        double tc = totalCount;
        Double num =Math.ceil(tc/size);//向上取整
        pageBean.setTotalPage(num.intValue());


        map.put("PageStart",(page-1)*size);
        map.put("PageSize", pageBean.getPageSize());
        map.put("PageSort", sort);
        map.put("PageAsc", asc);

        //封装每页显示的数据
        List<ExpressCompany> lists = expressCompanyDao.selectExpressCompanyByPage(map);
        pageBean.setLists(lists);
        return pageBean;
    }

    @Override
    public int selectCountByExpressCompany() {
        return expressCompanyDao.selectCountByExpressCompany();
    }

    @Override
    public void insertExpressCompanyType(ExpressCompany expressCompany) {
        expressCompanyDao.insertExpressCompanyType(expressCompany);
    }

    @Override
    public ExpressCompany selectByExpressId(int id) {
        return expressCompanyDao.selectByExpressId(id);
    }

    @Override
    public void updateExpressCompanyType(ExpressCompany expressCompany) {
        expressCompanyDao.updateExpressCompanyType(expressCompany);
    }

    @Override
    public void deleteExpressCompanyById(Integer id) {
        expressCompanyDao.deleteExpressCompanyById(id);
    }

    @Override
    public ExpressCompany selectByExpressCompanyType(String expressCompanyType) {
        return expressCompanyDao.selectByExpressCompanyType(expressCompanyType);
    }
}
